package com.xkc.androidgoogleplay

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import com.android.billingclient.api.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.properties.Delegates

class MainActivity : AppCompatActivity(), GoogleBillingUtil.OnPurchaseFinishedListener,
    GoogleBillingUtil.OnQueryFinishedListener, GoogleBillingUtil.OnStartSetupFinishedListener,
    GoogleBillingUtil.OnConsumeResponseListener {

    var googleBillingUtil : GoogleBillingUtil by Delegates.notNull()

    var productsAdapter : ProductsAdapter by Delegates.notNull()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val layoutManager = LinearLayoutManager(this)
        products.layoutManager=layoutManager
//        GoogleBillingUtil.setSkus(new String[]{"内购商品id"},new String[]{"订阅商品id"})//没有的时候传null或者new String[]{},而不是new String[]{""}
        //在GoogleBillingUtil.build之前调用。只需要设置一次。
        var  StringArray = arrayOf("get_1_coins","get_5_coins","get_10_coins")
        var  StringArray2 = arrayOf("get_3_coins","get_4_coins")
        GoogleBillingUtil.setSkus(StringArray,StringArray2)//没有的时候传null或者new String[]{},而不是new String[]{""}
        googleBillingUtil = GoogleBillingUtil.getInstance()
            .setOnPurchaseFinishedListener(this)//注意，监听器设置是可选的，视个人需求设置。
            .setOnQueryFinishedListener(this)
            .setOnStartSetupFinishedListener(this)
            .setOnConsumeResponseListener(this )
            .build(this)

        findViewById<View>(R.id.loadProducts).setOnClickListener(View.OnClickListener {
            googleBillingUtil.queryInventoryInApp()
        })
    }



    private fun initProductAdapter(skuDetailsList: List<SkuDetails>) {
        productsAdapter = ProductsAdapter(skuDetailsList) {
            googleBillingUtil.purchaseInApp(MainActivity@this,it.sku)
        }
        products.adapter = productsAdapter
    }

    override fun onConsumeSuccess(purchaseToken: String?) {
        Log.e("MainActivity", "onConsumeSuccess $purchaseToken")
    }

    override fun onConsumeFail(responseCode: Int) {
        Log.e("MainActivity", "onConsumeFail $responseCode")
    }

    override fun onSetupSuccess() {

        Log.e("MainActivity", "onSetupSuccess ")

    }

    override fun onSetupFail(responseCode: Int) {
        Log.e("MainActivity", "onSetupFail $responseCode")

    }

    override fun onSetupError() {
        Log.e("MainActivity", "onSetupError ")

    }

    override fun onQueryFail(responseCode: Int) {
        Log.e("MainActivity", "onQueryFail ")

    }

    override fun onQueryError() {
        Log.e("MainActivity", "onQueryError ")
    }

    override fun onQuerySuccess(skuType: String?, list: MutableList<SkuDetails>?) {

        Log.e("MainActivity", "onQuerySuccess $skuType")

        if (list !=null && skuType == BillingClient.SkuType.INAPP) {
            initProductAdapter(list)
        }

    }

    override fun onPurchaseSuccess(list: MutableList<Purchase>?) {

        Log.e("MainActivity","onPurchaseSuccess" + list!![0].sku)
//        initProductAdapter(list!!)

    }

    override fun onPurchaseFail(responseCode: Int) {
        Log.e("MainActivity", "responseCode $responseCode")

    }

    override fun onPurchaseError() {
        Log.e("MainActivity", "onPurchaseError ")

    }

    override fun onBackPressed() {
        GoogleBillingUtil.cleanListener()
        super.onBackPressed()
    }

}
